<?php

declare(strict_types=1);

namespace app\api\controller;

use app\api\service\DmjUserService;
use app\api\service\OilCardService;
use app\BaseController;
use app\model\OilCardOrder;
use app\model\UserOilCard;
use app\Request;

/** @apiDefine oilCardApi 手机端油卡Api */
class OilCard extends BaseController
{
    public $oilCardService;

    public function _initialize()
    {
        // 微客云 店铺信息
    }

    public function __construct(OilCardService $oilCardService)
    {
        $this->oilCardService = $oilCardService;

        request()->user = (new DmjUserService())->byMobile('18922396855', 1, 1)['data'];
    }

    /**
     * @api  {GET} /api/oilCard/home 油卡首页配置
     * @apiName userCardList 油卡首页配置
     * @apiGroup oilCardApi
     * @apiDescription 油卡首页配置
     * @apiParam {Number} cnid 渠道id
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function home(Request $request)
    {
        $list = [
            'goods' => $this->oilCardService->getGoods($request->cnid),
        ];

        return success('', $list);
    }

    /**
     * @api  {GET} /api/oilCard/userCardList 用户油卡列表
     * @apiName userCardList 用户油卡列表
     * @apiGroup oilCardApi
     * @apiDescription 用户油卡列表
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function userCardList(Request $request)
    {
        // 有分页
        if ($request->param('page')) {
            $list = UserOilCard::where('user_id', $request->user['id'])->paginate($request->only(['page' => 1, 'list_rows' => 15]));
        } else {
            $list = UserOilCard::where('user_id', $request->user['id'])->select();
            if (!$list->isEmpty()) {
                $list = $list->toArray();
            } else {
                $list = [];
            }
        }

        return success('', $list);
    }

    /**
     * @api  {POST} /api/oilCard/userCardAdd 油卡新增
     * @apiName userCardAdd 油卡新增 todo: 需要根据油卡类型去做不同供应商的添加 以适配不同供应商
     * @apiGroup oilCardApi
     * @apiDescription 油卡新增
     * @apiParam {String} card_num 卡号
     * @apiParam {String} name 姓名
     * @apiParam {String} phone 手机号
     * @apiParam {String} user_num 身份证号
     * @apiParam {Number=0,1} card_type 油卡类型 0中石化 1中石油
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function userCardAdd(Request $request)
    {
        $rules = [
            'card_num' => 'require',
            'name' => 'require',
            'phone' => 'require|mobile',
            'user_num' => 'require',
            'card_type' => 'require|in:0,1',
        ];
        $message = [
            'require' => '请填写必填参数',
            'mobile' => '手机号格式错误',
            'in' => '请选择油卡类型',
        ];

        $param = $request->only(array_keys($rules));
        if ($msg = dmjValidate($rules, $message, $param)) {
            return fail($msg);
        }

        $res = $this->oilCardService->addOilCard($param, $request->user['id']);
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }

    /**
     * @api  {POST} /api/oilCard/userCardInfo 油卡信息
     * @apiName userCardInfo 供应商处的油卡信息 todo: 需要根据油卡类型去做不同供应商的添加 以适配不同供应商
     * @apiGroup oilCardApi
     * @apiDescription 油卡新增
     * @apiParam {Number} id
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function userCardInfo(Request $request)
    {
        $res = $this->oilCardService->infoOilCard($request->param('id', 0));
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }

    /**
     * @api  {POST} /api/oilCard/userCardEdit 油卡编辑
     * @apiName userCardEdit 油卡编辑
     * @apiGroup oilCardApi
     * @apiDescription 油卡编辑
     * @apiParam {Number} id  列表返回的id
     * @apiParam {String} card_num 卡号
     * @apiParam {String} name 姓名
     * @apiParam {String} phone 手机号
     * @apiParam {String} user_num 身份证号
     * @apiParam {Number=0,1} card_type 油卡类型 0中石化 1中石油
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function userCardEdit(Request $request)
    {
        $rules = [
            'id' => 'require',
            'card_num' => 'require',
            'name' => 'require',
            'phone' => 'require|mobile',
            'user_num' => 'require',
            'card_type' => 'require|in:0,1',
        ];
        $message = [
            'require' => '请填写必填参数',
            'mobile' => '手机号格式错误',
            'in' => '请选择油卡类型',
        ];

        $param = $request->only(array_keys($rules));
        if ($msg = dmjValidate($rules, $message, $param)) {
            return fail($msg);
        }

        $id = $param['id'];
        unset($param['id']);
        $res = $this->oilCardService->editOilCard($id, $param, $request->user['id']);
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }

    /**
     * @api  {POST} /api/oilCard/userCardDelete 油卡删除
     * @apiName userCardDelete 油卡删除
     * @apiGroup oilCardApi
     * @apiDescription 油卡删除
     * @apiParam {Number} id id 列表返回的id
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function userCardDelete(Request $request)
    {
        if (!$id = $request->param('id', 0)) {
            return fail('非法参数');
        }
        $res = $this->oilCardService->deleteOilCard($id, $request->user['id']);
        if ($res['code']) {
            return fail($res['msg']);
        }

        return success();
    }

    /**
     * @api  {POST} /api/oilCard/prepay 油卡充值预付
     * @apiName oilCardPrepay 油卡充值预付
     * @apiGroup oilCardApi
     * @apiDescription 油卡充值预付
     * @apiParam {String} oil_card_id  油卡列表返回的id
     * @apiParam {Number=1,2} pay_type 支付方式 1微信支付 2支付宝支付
     * @apiParam {Number} origin_amount 充值金额
     * @apiParam {Number} amount 付费金额
     * @apiParam {Number=1,2} recharge_type 快慢充根据首页配置里商品列表信息的 recharge_type， 1慢充 2快充
     * @apiParam {String} cnid 渠道id
     * @apiUse  AuthHeader
     * @apiUse  BaseReturn
     * @apiVersion 1.0.0
     */
    public function prepay(Request $request)
    {
        $rules = [
            'oil_card_id' => 'require|numeric',
            'pay_type' => 'require|in:1,2',
            'origin_amount' => 'require|numeric|in:'.implode(',', $this->oilCardService->amountList),
            'amount' => 'require|numeric',
            'cnid' => 'require|numeric',
        ];
        $message = [
            'require' => '请填写必填参数',
            'numeric' => '免费格式错误',
            'pay_type.in' => '非法支付方式',
            'origin_amount.in' => '非法充值面额，请选择或者刷新页面',
        ];

        $param = $request->only(array_keys($rules));
        if ($msg = dmjValidate($rules, $message, $param)) {
            return fail($msg);
        }
        // 做预支付 入库订单表
        $res = $this->oilCardService->prepay($param, $request->user['id']);
    }

    /**
     * @api  {POST} /api/oilCard/order 油卡订单
     * @apiName oilCardPrepay 油卡订单
     * @apiGroup oilCardApi
     * @apiDescription 油卡订单 todo 待完善，不同供应商对应的订单商品可能不一致
     * @apiParam {String} [sn] 订单号
     * @apiParam {Number=0,1,2} [status] 快慢充根据首页配置里商品列表信息的 recharge_type， 1 慢充 2快充
     * @apiParam {String} cnid 渠道id
     * @apiParam {Number} page
     * @apiParam {Number}list_rows
     * @apiUse  AuthHeader
     * @apiUse  PageReturn
     * @apiVersion 1.0.0
     */
    public function order(Request $request)
    {
        $alias = 'order';
        $param = $request->only(['cnid', 'status', 'sn']);
        $where = [
            [$alias.'.serv_type', '=', PRODUCT_OILCARD_SERV],
            [$alias.'.user_id', '=', $request->user['id']],
        ];
        if (!isset($param['status'])) {
            $query = OilCardOrder::as($alias)->withJoin(['oilCard'])->where($where);
        } else {
            $query = OilCardOrder::as($alias)->withJoin(['oilCard' => function ($query) use ($param) {
                $query->where('status', $param['status']);
            }])->where($where);
        }

        if (empty($param['sn'])) {
            $query = $query->whereLike('sn', '%'.$param['sn'].'%');
        }

        return success('', $query->paginate($request->only(['page' => 1, 'list_rows' => 10]))->toArray());
    }
}
